//
//  BannnerTableViewCell.m
//  supplyerIOS
//
//  Created by UaenaMo on 2018/5/31.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "BannnerTableViewCell.h"

@interface BannnerTableViewCell()<UIScrollViewDelegate>
{
    UIScrollView * picScrollview;
//    UIPageControl * pageControl;
    NSTimer * pageTimer;
    NSArray * dataArr;
}
@end

@implementation BannnerTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        picScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth/375 * 200)];
        picScrollview.delegate = self;
        picScrollview.pagingEnabled = YES;
        picScrollview.contentSize = CGSizeMake(ScreenWidth, ScreenWidth/375 * 200);
        picScrollview.showsHorizontalScrollIndicator = NO;
        picScrollview.scrollsToTop = NO;
        [self addSubview:picScrollview];
    }
    return self;
}

+(CGFloat)height
{
    return ScreenWidth/375 * 200;
}

-(void)loadData:(NSArray *)data
{
    dataArr = data;
    if (pageTimer)
    {
        [pageTimer invalidate];
//        [pageControl removeFromSuperview];
        [picScrollview setContentOffset:CGPointMake(0, 0) animated:NO];
    }
    pageTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(rotate) userInfo:nil repeats:YES];
    
//    pageControl = [[UIPageControl alloc]initWithFrame:CGRectZero];
//    pageControl.pageIndicatorTintColor = [UIColor grayColor];
//    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
//    [pageControl setNumberOfPages:[data count]];
//    [pageControl sizeToFit];
//    pageControl.layer.position = CGPointMake(ScreenWidth/2, ScreenWidth/375 * 200 - 20);
//    [self addSubview:pageControl];
    
    for (UIImageView * view in picScrollview.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            [view removeFromSuperview];
        }
    }
    picScrollview.contentSize = CGSizeMake(ScreenWidth * [data count], ScreenWidth/375 * 200);
    for (int i = 0; i < [data count]; i ++)
    {
        UIImageView * picView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth * i, 0, ScreenWidth, ScreenWidth/375 * 200)];
        picView.tag = i;
        picView.contentMode = UIViewContentModeScaleAspectFill;
        picView.clipsToBounds = YES;
        picView.userInteractionEnabled = YES;
        picView.image = [UIImage imageNamed:@"test"];
        [picScrollview addSubview:picView];
        
        NSDictionary * picData = [data objectAtIndex:i];
        
        NSString * bannerUrl = [NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[[data objectAtIndex:i]objectForKey:@"Pic"]];
        
        [picView sd_setImageWithURL:[NSURL URLWithString:bannerUrl]];
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapped:)];
        [picView addGestureRecognizer:tap];
    }
}

-(void)rotate
{
    if (picScrollview.contentOffset.x < picScrollview.contentSize.width - ScreenWidth) {
        [picScrollview setContentOffset:CGPointMake(picScrollview.contentOffset.x + ScreenWidth, 0) animated:YES];
    }else
    {
        [picScrollview setContentOffset:CGPointMake(0, 0) animated:YES];
//        pageControl.currentPage = 0;
        
    }
}

#pragma mark - scrollviewdelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    pageControl.currentPage = scrollView.contentOffset.x/ScreenWidth;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [pageTimer invalidate];
    
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    pageTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(rotate) userInfo:nil repeats:YES];
}

#pragma mark - tapAction

-(void)tapped:(UITapGestureRecognizer *)tapped
{
    [self.delegate selectedBanner:[dataArr objectAtIndex:tapped.view.tag]];
}

-(void)dealloc
{
    [pageTimer invalidate];
}


@end
